package cn.trs.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version 1.0
 * @author:TRS
 * @create:2023/2/23 9:58
 * @description:
 * @ClassName: StringUtil
 * @company TRS
 */
public class StringUtil extends org.apache.commons.lang3.StringUtils {

    public static boolean validateParams(Object... objects) {
        boolean a = true;
        for (Object obj : objects) {
            if (StringUtils.isEmpty(obj)) {
                a = false;
            }
        }
        return a;
    }

    public static String getUuid() {
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        String uuidStr = str.replace("-", "");
        return uuidStr;
    }

    public static int getRadom(){
        SecureRandom rand = new SecureRandom();
        // Setting the upper bound to generate
        // the random numbers between the specific range
        int upperbound = 2147483647;
        // Generating random values from 0 - 999
        // using nextInt()
        return rand.nextInt(upperbound);
    }

    public static List<String> string2List(String str, String mark) {
        String[] array = str.split(mark);
        List<String> stringList = Arrays.asList(array);
        return stringList;
    }

    public static boolean validateJson(String... strings) {
        boolean a = true;
        for (String obj : strings) {
            try {
                JSONObject jsonStr = JSONObject.parseObject(obj);
                a = true;
            } catch (Exception e) {
                try {
                    JSONArray jsonStr = JSONArray.parseArray(obj);
                    a = true;
                } catch (Exception ex) {
                    a = false;
                }
            }
        }
        return a;
    }

    public static String getIndexPreviewFileUrl(Map<String, String> previewFileUrl, String fileId, String... previewFileName){
        if(CollectionUtils.isEmpty(previewFileUrl) || previewFileName==null || previewFileName.length == 0){
            return null;
        }
        for (String pFileName: previewFileName) {
            for (Map.Entry<String, String> entry : previewFileUrl.entrySet()) {
                if(entry.getKey().endsWith(fileId+"/"+pFileName)){
                    return entry.getValue();
                }
            }

        }
        return null;
    }

    /**
     * 判断是否为整数
     * @param str
     * @return
     */
    public static boolean isInt(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断是否为浮点数
     * @param str
     * @return
     */
    public static boolean isFloat(String str) {
        try {
            Float.parseFloat(str);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断是否为bool
     * @param str
     * @return
     */
    public static boolean isBool(String str) {
        if ("false".equals(str)) {
            return true;
        }
        return Boolean.parseBoolean(str);
    }

    /**
     * Java 正则 去前后空格 回车
     * @param str
     * @return
     */
    public static String getStringNoBlank(String str) {
        if(str!=null && !"".equals(str)) {
            str.replaceAll("\n", "");
            Pattern p = Pattern.compile("(^\\s*)|(\\s*$)");
            Matcher m = p.matcher(str);
            String strNoBlank = m.replaceAll("");
            return strNoBlank;
        }else {
            return  "";
        }
    }

    public static boolean isChinese(char c) {
        return c >= 0x4e00 && c <= 0x9fa5;
    }
}
